﻿Imports Microsoft.Phone.Tasks
Imports System.Threading

Partial Public Class MainPage
    Inherits PhoneApplicationPage

    Public Sub New()
        InitializeComponent()
        ' Set the data context of the listbox control to the sample data
        Me.DataContext = Me

        Dim fi As New FeedItem()
        fi.Title = "Loading"
        fi.Description = ""

        Dim lfi As New List(Of FeedItem)()
        lfi.Add(fi)

        Me.Twitter.ItemsSource = lfi
        Me.Blog.ItemsSource = lfi

        BlogRefresh_Click(Nothing, Nothing)
        TwitterRefresh_Click(Nothing, Nothing)
    End Sub

    Private progressBarCounter As Integer = 0

    Public Sub ShowProgressBar(ByVal show As Boolean)
        If show = True Then
            Interlocked.Increment(progressBarCounter)
        Else
            Interlocked.Decrement(progressBarCounter)
            Interlocked.CompareExchange(progressBarCounter, 0, -1)
        End If

        If progressBarCounter = 0 Then
            Me.ProgressBar.IsIndeterminate = False
            Me.ProgressBar.Visibility = Visibility.Collapsed
        Else
            Me.ProgressBar.IsIndeterminate = True
            Me.ProgressBar.Visibility = Visibility.Visible
        End If
    End Sub

    Protected Overrides Sub OnNavigatingFrom(ByVal e As System.Windows.Navigation.NavigatingCancelEventArgs)
        Me.State("Panorama") = Me.panorama.SelectedIndex
        MyBase.OnNavigatingFrom(e)
    End Sub

    Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)
        If Me.State.ContainsKey("Panorama") Then
            Me.panorama.DefaultItem = Me.panorama.Items(CInt(Me.State("Panorama")))
            MyBase.OnNavigatedTo(e)
        End If
    End Sub
    Private Sub listbox_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        If e.AddedItems.Count > 0 Then
            Dim item = CType(e.AddedItems(0), FeedItem)
            Dim wbt As New WebBrowserTask
            wbt.URL = item.Link
            wbt.Show()

        End If
    End Sub

    Private Sub BlogRefresh_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim rssClient As New RssClient("http://twitter.com/statuses/user_timeline/185968571.rss", Me.Blog) 'mettere il proprio blog
        AddHandler rssClient.ItemsReceived, AddressOf rssClient_ItemsReceived
        rssClient.LoadItems()
        Me.ShowProgressBar(True)
    End Sub

    Private Sub TwitterRefresh_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim twiClient As New RssClient("http://twitter.com/statuses/user_timeline/185968571.rss", Me.Twitter) 'mettere l'RSS del proprio twitter
        AddHandler twiClient.ItemsReceived, AddressOf rssClient_ItemsReceived
        twiClient.LoadItems()
        Me.ShowProgressBar(True)
    End Sub

    Private Sub Marketplace_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim mr As New MarketplaceSearchTask()
        mr.SearchTerms = "BlahBlahBlah" 'mettere il proprio nome del Marketplace
        mr.Show()
    End Sub

    Private Sub Email_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim email As New EmailComposeTask
        email.To = "MyWp7Brand@blah.blah" 'mettere la propria mail
        email.Subject = "Mail da My Wp7 Brand App"
        email.Show()
    End Sub

    Private Sub rssClient_ItemsReceived(ByVal client As RssClient, ByVal items As IList(Of FeedItem), ByVal control As Object)
        Dispatcher.BeginInvoke(Sub()
                                   If items IsNot Nothing Then
                                       CType(control, ItemsControl).ItemsSource = items
                                   Else
                                       If (CType(control, ItemsControl).Items.Count = 1) Then
                                           Dim fi As New FeedItem
                                           fi.Title = "No connectoin"
                                           fi.Description = ""
                                           Dim lfi As New List(Of FeedItem)
                                           lfi.Add(fi)
                                           CType(control, ItemsControl).ItemsSource = lfi
                                       End If
                                       Me.ShowProgressBar(False)
                                   End If
                               End Sub)
    End Sub
End Class
